package cash_access;

import mware_lib.communication.Client;
import mware_lib.exceptions.RemoteException;
import mware_lib.messages.ReqAccMsg;
import mware_lib.messages.RetAccMsg;


public class AccountProxy extends Account{
	private String skelHost;
	private int skelPort;
	private String ktoID;

	public AccountProxy( String ktoID, String host, int port) {
		this.ktoID = ktoID;
		this.skelHost = host;
		this.skelPort = port;
	}

	@Override
    public void deposit(double amount) {
            
            try {
                    Client client = new Client(skelHost,skelPort);
                    ReqAccMsg reqMsg = new ReqAccMsg(this.ktoID, "deposit", amount);
                    client.send(reqMsg);
                    
                    Object recv = client.receive();
                    if(!(recv instanceof RetAccMsg)){
                            throw new RemoteException("Received object from no instance of RetAccMsg");
                    }
                    recv = ((RetAccMsg)recv).getResult();
                    if(!(recv == null)) {
                            throw (Exception)recv;
                    }
            } catch (RuntimeException re){	//RuntimeException an den Benutzer weiterleiten
    			throw re;
            } catch (Exception e) {
                    //e.printStackTrace();
                    System.out.println((e.getClass().toString() + ": " + e.getMessage()));
            }               
    }

    @Override
    public void withdraw(double amount) throws OverdraftException {
            
            try {
            	Client client = new Client(skelHost,skelPort);
            	ReqAccMsg reqMsg = new ReqAccMsg(this.ktoID, "withdraw", amount);
                    client.send(reqMsg);
                    
                    Object recv = client.receive();
                    if(!(recv instanceof RetAccMsg)){
                            throw new RemoteException("Received object from no instance of RetAccMsg");
                    }
                    recv = ((RetAccMsg)recv).getResult();
                    if(!(recv == null)) {
                            if(recv instanceof OverdraftException){
                                    throw ((OverdraftException)recv);
                            }else{
                                    throw (Exception)recv;
                            }
                    }
            } catch (OverdraftException e) {
                throw e;
            } catch (RuntimeException re){	//RuntimeException an den Benutzer weiterleiten
    			throw re;
            } catch (Exception e) {
                    //e.printStackTrace();
                    System.out.println((e.getClass().toString() + ": " + e.getMessage()));
            }                       
    }

    @Override
    public double getBalance() {
            
            double ret = 0;
            
            try {
            		Client client = new Client(skelHost,skelPort);
            		ReqAccMsg reqMsg = new ReqAccMsg(this.ktoID, "getBalance");
                    client.send(reqMsg);
                    
                    Object recv = client.receive();
                    if(!(recv instanceof RetAccMsg)){
                            throw new RemoteException("Received object from no instance of RetAccMsg");
                    }
                    recv = ((RetAccMsg)recv).getResult();
                    if(!(recv instanceof Double)) {
                            throw (Exception)recv;
                    }
                    ret = ((Double)recv);
                  
            } catch (RuntimeException re){	//RuntimeException an den Benutzer weiterleiten
    			throw re;
            } catch (Exception e) {
                    //e.printStackTrace();
                    System.out.println((e.getClass().toString() + ": " + e.getMessage()));
            }
            return ret;
    }

}
